﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.DataVisualization.Charting;

using Wicresoft.Common;
using Wicresoft.JDPostBoard.Entity;
using System.Text;
using System.IO;

public partial class Car_UseList : BaseAuthenticationPage
{

    protected DataTable DTData
    {
        get
        {
            if (ViewState["DTData"] == null)
                ViewState["DTData"] = new DataTable();
            return ViewState["DTData"] as DataTable;
        }
        set
        {
            ViewState["DTData"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtStart.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            this.txtEnd.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        TextWriter tw = new StringWriter(sb);
        HtmlTextWriter w = new HtmlTextWriter(tw);
        this.divStatic.RenderControl(w);

        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        //Response.Buffer = false;
        Response.BufferOutput = true;

        Response.Charset = "utf-8";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("车辆使用明细表") + ".xls");
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.ContentType = "application/ms-excel";

        byte[] bs = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sb.ToString());

        byte[] outer = new byte[bs.Length + 3];
        outer[0] = (byte)0xef;
        outer[1] = (byte)0xbb;
        outer[2] = (byte)0xbf;
        Array.Copy(bs,0, outer, 3, bs.Length);
        Response.BinaryWrite(outer);
        Response.Flush();
        Response.End();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.txtStart.Text.Trim() == "")
        {
            WebCommon.ShowMessage(this, "请输入起始时间");
            this.txtStart.Focus();
            return;
        }

        if (this.txtEnd.Text.Trim() == "")
        {
            WebCommon.ShowMessage(this, "请输入截至时间");
            this.txtEnd.Focus();
            return;
        }
        BindData();
    }

    private void BindData()
    {
        int recordCount = 0;
        string orderString = "startdate";
        string where = " StartDate>='" + this.txtStart.Text + "' and StartDate<'" + this.txtEnd.Text + "' and StatusCN=N'审核通过' ";
        DTData = DataAccess.Select(typeof(vwVehicleApply2), 0, 9999, where, orderString, ref recordCount);
        rptOrder.DataSource = DTData;
        rptOrder.DataBind();

    }
}